package com.atguigu.dga.governance.assessor.spec;

import com.atguigu.dga.constant.DgaConstant;
import com.atguigu.dga.governance.assessor.Assessor;
import com.atguigu.dga.governance.bean.AssessParam;
import com.atguigu.dga.governance.bean.GovernanceAssessDetail;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.text.ParseException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * ClassName: TableNameStandard
 * Package: com.atguigu.dga.governance.assessor.spec
 * Description:
 *
 * @Author:
 * @Create: 2024/3/18 14:00
 * @Version: 1.0
 */
@Component("TABLE_NAME_STANDARD")
public class TableNameStandard extends Assessor {
    Pattern odsPattern = Pattern.compile("^ods_\\w+_(inc|full)$") ;
    Pattern dimPattern = Pattern.compile("^dim_\\w+(_(zip|full))?$") ;
    Pattern dwdPattern = Pattern.compile("^dwd_\\w+_\\w+_(acc|inc|full)$") ;
    Pattern dwsPattern = Pattern.compile("^dws_\\w+_\\w+_\\w+_(\\d{1,2}d|nd|td)$") ;
    Pattern adsPattern = Pattern.compile("^ads_\\w+$") ;
    Pattern dmPattern = Pattern.compile("^dm_\\w+$") ;
    @Override
    public void checkProblem(AssessParam assessParam, GovernanceAssessDetail governanceAssessDetail) throws ParseException {
        String tableName = assessParam.getTableMetaInfo().getTableName();
        String dwLevel = assessParam.getTableMetaInfo().getTableMetaInfoExtra().getDwLevel();

        Matcher matcher = null;
        if(DgaConstant.DW_LEVEL_ODS.equals(dwLevel)) {
            matcher = odsPattern.matcher(tableName);
        }else if(DgaConstant.DW_LEVEL_DIM.equals(dwLevel)) {
            matcher = dimPattern.matcher(tableName);
        }else if(DgaConstant.DW_LEVEL_DWD.equals(dwLevel)) {
            matcher = dwdPattern.matcher(tableName);
        }else if(DgaConstant.DW_LEVEL_DWS.equals(dwLevel)){
            matcher = dwsPattern.matcher((tableName));
        }else if(DgaConstant.DW_LEVEL_ADS.equals(dwLevel)) {
            matcher = adsPattern.matcher(tableName);
        } else {
            governanceAssessDetail.setAssessScore(BigDecimal.valueOf(5));
            governanceAssessDetail.setAssessProblem("未纳入分层");
        }
        if(matcher != null && !matcher.matches()) {
            governanceAssessDetail.setAssessScore(BigDecimal.ZERO);
            governanceAssessDetail.setAssessProblem("表名不符合规范");
        }




    }
}
